﻿#include "cPlayer.h"
#include "mediaDemuxing.h"
#include "mediaStreamIO.h"
#include "mediaDecode.h"
#include "ioQueue.h"
#include "global.h"
#include "error.h"

#include <QDebug>

#include <crtdbg.h>
#include <assert.h>

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

class cPlayerPrivate
{
public:
    cPlayerPrivate() = default;

    QString     m_currentMedia;
    QStringList m_currentMediaList;
    int         m_currentPos;
    int         m_height;
    int         m_width;

    cPlayer::State    m_state = cPlayer::State::noPlay;
    QVector<QWidget*> m_outWidget;
    QVector<cStream*> m_stream;

    mediaDemuxing*    m_demuxing    = NULL;
    mediaStreamIO*    m_inputStream = NULL;
    mediaDecode*      m_decode      = NULL;
    ioQueue*          m_ioQueue     = NULL;
};

cPlayer::cPlayer(QObject *parent) :
    QObject(parent), d(new cPlayerPrivate)
{
}

cPlayer::~cPlayer()
{
    stop();
    delete d;
}

bool cPlayer::start()
{
    if("" == d->m_currentMedia) {
        _errstr = "未设置媒体路径";
        return false;
    }

    d->m_demuxing    = new mediaDemuxing;
    d->m_inputStream = new mediaStreamIO;
    d->m_decode      = new mediaDecode;
    d->m_ioQueue     = new ioQueue;

    if(!d->m_demuxing->open(d->m_currentMedia)) {
        _errstr = "文件打开失败";
        return false;
    }

    d->m_inputStream->setFormat(d->m_demuxing->getFormat());
    d->m_inputStream->start();

    if (NULL == (d->m_ioQueue = d->m_inputStream->getQueue())) {
        _errstr = "包队列获取失败";
        return false;
    }

    d->m_decode->setIOQueue(d->m_ioQueue);
    d->m_decode->setFormat(d->m_demuxing->getFormat());
    if(!d->m_decode->startDecode()) {
        _errstr = "无法开始解码";
        return false;
    }

    d->m_state = State::Playing;

    return true;
}

bool cPlayer::stop()
{
    if(d->m_state == State::noPlay || d->m_state == State::Stop)
        return true;
    else
        d->m_state = State::Stop;

    d->m_decode->stopDecode();
    d->m_inputStream->stopRead();

    if(d->m_demuxing) {
        delete d->m_demuxing   ;
        d->m_demuxing    = NULL;
    }

    if(d->m_inputStream) {
        delete d->m_inputStream;
        d->m_inputStream = NULL;
    }

    if(d->m_decode) {
        delete d->m_decode     ;
        d->m_decode      = NULL;
    }

    return true;
}

void cPlayer::pause()
{
    d->m_state = State::Pause;
}

bool cPlayer::seek(__int64 pos)
{
    d->m_inputStream->seek(pos);
    return true;
}

void cPlayer::appendMediaList(QStringList list)
{
    Q_UNUSED(list);
}

void cPlayer::appendMediaFile(QString file)
{
    Q_UNUSED(file);
}

int  cPlayer::currentPos()
{
    return 0;
}

int  cPlayer::duration()
{
    return 0;
}

void cPlayer::setMediaList(QStringList list)
{
    Q_UNUSED(list);
}

void cPlayer::setMediaFile(QString file)
{
    d->m_currentMedia = file;
}

void cPlayer::setOutWidget(QWidget *w)
{
    Q_UNUSED(w);
}

cPlayer::State cPlayer::state()
{
    return cPlayer::State::Pause;
}


